var app = new Vue({
    el: '#app',
    data: {
        userId: 0,
        name: '',
        location: '',
        address: '苏州',
        contacts: '',
        phone: '',
        mobile: '',
        nature: '',
        number: '0',
        imglist1: ['http://appimg.yinsoso.com/yinsoso_logo.png'],
        headimg: 'http://appimg.yinsoso.com/yinsoso_logo.png',
        imglist2: ['http://appimg.yinsoso.com/yinsoso_logo.png'],
        imglist3: [],
        licenseimg: '',
        accept: 'image/gif, image/jpeg, image/png, image/jpg',
        isAllowUpload: true,
    },
    created() {
        if ($.getPara("userId") != "") {
            var userId = $.getPara("userId");
            this.userId = userId;
            $.setStorage("userId", userId)
        } else {
            if ($.getStorage("userId") && $.getStorage("userId") != "") {
                var userId = parseInt($.getStorage("userId"));
                this.userId = userId
            }
        }
    },
    methods: {
        compress(event, callback) {
            if (typeof (FileReader) === 'undefined') {
                $.toast("当前浏览器内核不支持base64图片压缩", "text")
            } else {
                try {
                    var file = event.currentTarget.files[0];
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        var image = $('<img/>');
                        image.on('load', function() {
                            var square = 500;
                            var canvas = document.createElement('canvas');
                            canvas.width = square;
                            canvas.height = square;
                            var context = canvas.getContext('2d');
                            context.clearRect(0, 0, square, square);
                            var imageWidth;
                            var imageHeight;
                            var offsetX = 0;
                            var offsetY = 0;
                            if (this.width > this.height) {
                                imageWidth = Math.round(square * this.width / this.height);
                                imageHeight = square;
                                offsetX = -Math.round((imageWidth - square) / 2)
                            } else {
                                imageHeight = Math.round(square * this.height / this.width);
                                imageWidth = square;
                                offsetY = -Math.round((imageHeight - square) / 2)
                            }
                            context.drawImage(this, offsetX, offsetY, imageWidth, imageHeight);
                            var data = canvas.toDataURL('image/jpeg');
                            callback(data)
                        });
                        image.attr('src', e.target.result)
                    }
                    ;
                    reader.readAsDataURL(file)
                } catch (e) {
                    $.toast("图片压缩失败，请重试！", "text")
                }
            }
        },
        LogoImgs(event) {
            var _event = event;
            var _this = this;
            let reader = new FileReader();
            let img1 = event.target.files[0];
            let type = img1.type;
            let size = img1.size;
            const files = event.target;
            if (this.accept.indexOf(type) == -1) {
                $.toast("请选择gif,jpeg,png,jpg格式图片上传", "text");
                return false
            } else if (size > 3145728) {
                $.toast("请选择3M以内的图片上传", "text");
                return false
            } else if (typeof (FileReader) === 'undefined') {
                $.toast("抱歉，你的浏览器不支持 FileReader，请使用现代浏览器操作！", "text")
            } else {
                _this.compress(event, function(base6464) {
                    var base6464 = base6464;
                    $.ajax({
                        url: '/api/common/upload/base64',
                        type: 'post',
                        data: {
                            base64: base6464
                        },
                        dataType: 'json',
                        success: function(response) {
                            if (response.code == 200) {
                                $.toast('恭喜你，图片上传成功', "text");
                                _this.imglist1.pop(response.data);
                                _this.imglist1.push(response.data);
                                _this.headimg = response.data;
                                _this.checkLimit()
                            } else {
                                $.toast(response.msg, "text")
                            }
                        }
                    })
                })
            }
        },
        companyImg(event) {
            var _event = event;
            var _this = this;
            let reader = new FileReader();
            let img1 = event.target.files[0];
            let type = img1.type;
            let size = img1.size;
            const files = event.target;
            if (this.accept.indexOf(type) == -1) {
                $.toast("请选择gif,jpeg,png,jpg格式图片上传", "text");
                return false
            } else if (size > 3145728) {
                $.toast("请选择3M以内的图片上传", "text");
                return false
            } else if (typeof (FileReader) === 'undefined') {
                $.toast("抱歉，你的浏览器不支持 FileReader，请使用现代浏览器操作！", "text")
            } else {
                _this.compress(event, function(base6464) {
                    var base6464 = base6464;
                    $.ajax({
                        url: '/api/common/upload/base64',
                        type: 'post',
                        data: {
                            base64: base6464
                        },
                        dataType: 'json',
                        success: function(response) {
                            if (response.code == 200) {
                                $.toast('恭喜你，图片上传成功', "text");
                                _this.imglist2.push(response.data);
                                _this.checkLimit()
                            } else {
                                $.toast(response.msg, "text")
                            }
                        }
                    })
                })
            }
        },
        licenseImg(event) {
            var _event = event;
            var _this = this;
            let reader = new FileReader();
            let img1 = event.target.files[0];
            let type = img1.type;
            let size = img1.size;
            const files = event.target;
            if (this.accept.indexOf(type) == -1) {
                $.toast("请选择gif,jpeg,png,jpg格式图片上传", "text");
                return false
            } else if (size > 3145728) {
                $.toast("请选择3M以内的图片上传", "text");
                return false
            } else if (typeof (FileReader) === 'undefined') {
                $.toast("抱歉，你的浏览器不支持 FileReader，请使用现代浏览器操作！", "text")
            } else {
                _this.compress(event, function(base6464) {
                    var base6464 = base6464;
                    $.ajax({
                        url: '/api/common/upload/base64',
                        type: 'post',
                        data: {
                            base64: base6464
                        },
                        dataType: 'json',
                        success: function(response) {
                            if (response.code == 200) {
                                $.toast('恭喜你，图片上传成功', "text");
                                _this.imglist3.pop(response.data);
                                _this.imglist3.push(response.data);
                                _this.licenseimg = response.data
                            } else {
                                $.toast(response.msg, "text")
                            }
                        }
                    })
                })
            }
        },
        delPicture(index, row, typeId) {
            this.idx = index;
            this.typeId = typeId;
            if (this.typeId == 1) {
                this.imglist1.splice(this.idx, 1);
                headimg = ''
            } else if (this.typeId == 2) {
                this.imglist2.splice(this.idx, 1)
            } else if (this.typeId == 3) {
                this.imglist3.splice(this.idx, 1)
            }
            this.checkLimit();
            $.toast('删除成功', "text")
        },
        checkLimit() {
            if (this.imglist2.length >= 5 || this.imglist3.length >= 5) {
                this.isAllowUpload = false
            } else {
                this.isAllowUpload = true
            }
        },
        submit: function() {
            if (this.name === '') {
                $.toast("请输入公司名称姓名", "text");
                return false
            }
            if (this.contacts === '') {
                $.toast("请输入联系人", "text");
                return false
            }
            if (this.phone === '') {
                $.toast("请输入手机号码", "text");
                return false
            } else if (!(/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.phone))) {
                $.toast("手机号码输入有误！", "text");
                return false
            }
            if (this.$refs.nature.value === '') {
                $.toast("请输入企业性质", "text");
                return false
            }
            if (this.imglist3.length < 1) {
                $.toast("请上传图片", "text");
                return false
            }
            var province = '江苏';
            var city = '苏州';
            var district = '吴中区';
            var userId = this.userId;
            var imgListUrl = JSON.stringify(this.imglist2);
            console.log(this.mobile);
            $.ajax({
                type: "post",
                url:  "api/user/enterprise/apply",
                data: {
                    userId: userId,
                    enterpriseName: this.name,
                    province: province,
                    city: city,
                    district: district,
                    address: this.address,
                    contact: this.contacts,
                    mobile: this.phone,
                    telephone: this.mobile,
                    staffNum: this.number,
                    enterpriseType: this.$refs.nature.value,
                    enterpriseLogo: this.headimg,
                    imgList: imgListUrl,
                    enterpriseLicense: this.licenseimg
                }
            }).done(function(data) {
                if (data.code == 200) {
                    $.toast("提交成功,请等待审核", "text");
                    setTimeout(function() {
                        var para = {};
                        para.isRefresh = true;
                        if (/(Android)/i.test(navigator.userAgent)) {
                            jsAndroid.appReturn(JSON.stringify(para))
                        } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                            window.webkit.messageHandlers.appReturn.postMessage(para)
                        } else {
                            window.location.href = para.url;
                            alert("PC浏览器")
                        }
                    }, 3000)
                } else {
                    $.toast(data.msg, 'text')
                }
            })
        }
    }
});
$(function() {
    $("#city-picker").cityPicker({
        title: "请选择地址",
        showDistrict: true,
        inputReadOnly: true
    });
    $("#picker").picker({
        title: "请选择企业性质",
        cols: [{
            textAlign: 'center',
            values: ['国有企业', '集体企业', '联营企业', '股份合作制企业', '私营企业', '个体户', '合伙企业', '有限责任公司股份有限公司']
        }],
        inputReadOnly: true
    })
})
